package com.persistence;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.jdo.Query;

import com.log.GPSLog;
import com.log.GPSLogEntry;


public class LogAccessObject extends DataAccessObject<GPSLogEntry> {
	private String appUsername;
	
	public void setAppUsername(String username){
		this.appUsername = username;
	}
	
	@Override
	public ArrayList<GPSLogEntry> getObjects() {
		getPersistenceManagerInstance();
		ArrayList<GPSLogEntry> logs = getLogList();
		closePersistenceManager();
		return logs;
	}

	@Override
	public GPSLogEntry getObject(String searchValue) {
		// TODO Auto-generated method stub
		getPersistenceManagerInstance();
		GPSLogEntry searchedLog = getLog(searchValue);
		closePersistenceManager();
		return searchedLog;
	}
	
	public ArrayList<GPSLogEntry> getObject(String username,Date searchingDate){
		getPersistenceManagerInstance();
		ArrayList<GPSLogEntry> logs = getLogList(username,searchingDate);
		closePersistenceManager();
		return logs;
	}
	
	@SuppressWarnings("unchecked")
	private ArrayList<GPSLogEntry> getLogList(String username,Date searchingDate){
		Query query = persistenceManager.newQuery(GPSLogEntry.class);
		query.setFilter("appUser == appUserParam && (logEntryDate >= startingHour && logEntryDate <= finishingHour)");
		query.setOrdering("logEntryDate ASC");
		Calendar calendarTime = Calendar.getInstance();
		calendarTime.setTime(searchingDate);
		calendarTime.set(Calendar.HOUR_OF_DAY, 00);
		calendarTime.set(Calendar.MINUTE, 00);
		Date startingTime = calendarTime.getTime();
		calendarTime.set(Calendar.HOUR_OF_DAY, 23);
		calendarTime.set(Calendar.MINUTE, 59);
		Date finishingTime = calendarTime.getTime();
		query.declareParameters("java.util.Date startingHour, java.util.Date finishingHour, String appUserParam");
		List<GPSLogEntry> listResult = (List<GPSLogEntry>)query.execute(startingTime,finishingTime,username);
		ArrayList<GPSLogEntry> logs = new ArrayList<GPSLogEntry>();
		for(GPSLogEntry logEntry : listResult){
			logs.add(logEntry);
		}
		return logs;
	}
	
	@SuppressWarnings("unchecked")
	private GPSLogEntry getLog(String username){
		Query query = persistenceManager.newQuery(GPSLog.class);
		query.setFilter("appUser == appUserParam");
		query.declareParameters("String appUserParam");
		List<GPSLogEntry> logList = (List<GPSLogEntry>)query.execute(username);
		GPSLogEntry searchedEntry = null;
		if(logList.size() > 0){
			searchedEntry = logList.get(0);
		}
		return searchedEntry;
	}
	
	@SuppressWarnings("unchecked")
	private ArrayList<GPSLogEntry> getLogList(){
		Query query = persistenceManager.newQuery(GPSLogEntry.class);
		query.setFilter("appUser == appUserParam");
		query.declareParameters("String appUserParam");
		List<GPSLogEntry> logEntry = (List<GPSLogEntry>)query.execute(appUsername);
		ArrayList<GPSLogEntry> arrayList = new ArrayList<GPSLogEntry>();
		for(GPSLogEntry entry : logEntry){
			arrayList.add(entry);
		}
		return arrayList;
	}

}
